import { http } from "@/utils/http";
import type { ListOption, StatefulSet, StatefulSetList } from "./types";
import type { BaseResponse } from "@/api/response";
import {
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq
} from "./apiHelper";
export type StatefulSetQuery = ListOption & {
  namespace?: string;
  name?: string;
};

/**
 * K8sStatefulSet 类用于处理 Kubernetes StatefulSet 相关的操作
 */
export class K8sStatefulSet {
  baseReq: MakePathReq = {
    src: "statefulsets",
    app: "apps/v1"
  };

  newEntity = (): StatefulSet => ({
    apiVersion: this.baseReq.app,
    kind: "StatefulSet",
    metadata: {
      name: "",
      namespace: "",
      labels: {},
      annotations: {}
    },
    spec: {}
  });

  getStatefulSet = (req?: MergePathReq): BaseResponse<StatefulSetList> => {
    const api = k8sPathMergeReqs(req, this.baseReq);
    return http.get(api, { params: req.query });
  };

  getStatefulSetDetail = (
    req?: MergePathReq,
    data?: StatefulSet
  ): BaseResponse<StatefulSet> => {
    const api = k8sPathMergeReqs(req, this.baseReq, data);

    return http.get(api);
  };

  updateStatefulSet = (
    req?: MergePathReq,
    data?: StatefulSet
  ): BaseResponse => {
    const api = k8sPathMergeReqs(req, this.baseReq, data);
    return http.put(api, { data });
  };

  deleteStatefulSet = (
    req?: MergePathReq,
    data?: StatefulSet
  ): BaseResponse => {
    const api = k8sPathMergeReqs(req, this.baseReq, data);

    return http.delete(api, { data });
  };

  createStatefulSet = (
    req?: MergePathReq,
    data?: StatefulSet
  ): BaseResponse => {
    const api = k8sPathMergeReqs(req, this.baseReq, data);

    return http.post(api, { data });
  };

  patchStatefulSet = (req?: MergePathReq, data?: StatefulSet): BaseResponse => {
    const api = k8sPathMergeReqs(req, this.baseReq, data);
    return http.patch(api, { data });
  };
}
